package com.jhy.new2025.BinaryTree;

import java.util.Deque;
import java.util.LinkedList;

/**
 * 给你一个二叉树的根节点 root ，
 * 检查它是否轴对称。
 */
public class IsSymmetric {
    public boolean isSymmetric(TreeNode root) {
        if (root == null) return true;
        // 这里需要用到 Deque<TreeNode> deque = new LinkedList<>();
        // 不能用ArrayDeque因为不支持null型 会报空指针异常
        Deque<TreeNode> deque = new LinkedList<>();
        deque.offer(root.left);
        deque.offer(root.right);
        while (!deque.isEmpty()) {
            TreeNode left = deque.poll();
            TreeNode right = deque.poll();
            if (left == null && right == null) {
                continue;
            }
            ;
            if (left == null || right == null || (right.val != left.val)) {
                return false;
            }
            deque.offer(left.left);
            deque.offer(right.right);
            deque.offer(left.right);
            deque.offer(right.left);
        }
        return true;
    }
}
